package com.uwlaser.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.uwlaser.base.PageInfo;
import com.uwlaser.base.QueryHelpMybatisPlus;
import com.uwlaser.base.impl.CommonServiceImpl;
import com.uwlaser.domain.PlanMixedLine;
import com.uwlaser.domain.PlanMixedRule;
import com.uwlaser.service.PlanMixedLineService;
import com.uwlaser.service.dto.PlanMixedLineDto;
import com.uwlaser.service.dto.PlanMixedLineQueryParam;
import com.uwlaser.service.mapper.PlanMixedLineMapper;
import com.uwlaser.service.mapper.PlanMixedRuleMapper;
import com.uwlaser.utils.ConvertUtil;
import com.uwlaser.utils.PageUtil;
import com.uwlaser.utils.enums.DictValueEnum;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
* @author zk-Qin
* @date 2022-05-11
*/
@Service
@AllArgsConstructor
// @CacheConfig(cacheNames = PlanMixedLineService.CACHE_KEY)
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class PlanMixedLineServiceImpl extends CommonServiceImpl<PlanMixedLineMapper, PlanMixedLine> implements PlanMixedLineService {

    // private final RedisUtils redisUtils;
    private final PlanMixedLineMapper planMixedLineMapper;

    private final PlanMixedRuleMapper planMixedRuleMapper;

    @Override
    public PageInfo<PlanMixedLineDto> queryAll(PlanMixedLineQueryParam query, Pageable pageable) {
        IPage<PlanMixedLine> queryPage = PageUtil.toMybatisPage(pageable);
        IPage<PlanMixedLine> page = planMixedLineMapper.selectPage(queryPage, QueryHelpMybatisPlus.getPredicate(query));
        return ConvertUtil.convertPage(page, PlanMixedLineDto.class);
    }

    @Override
    public List<PlanMixedLineDto> queryAll(PlanMixedLineQueryParam query){
        return ConvertUtil.convertList(planMixedLineMapper.selectList(QueryHelpMybatisPlus.getPredicate(query)), PlanMixedLineDto.class);
    }

    @Override
    public PlanMixedLine getById(Integer id) {
        return planMixedLineMapper.selectById(id);
    }

    @Override
    // @Cacheable(key = "'id:' + #p0")
    public PlanMixedLineDto findById(Integer id) {
        return ConvertUtil.convert(getById(id), PlanMixedLineDto.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(PlanMixedLineDto resources) {
        PlanMixedLine entity = ConvertUtil.convert(resources, PlanMixedLine.class);
        //如果是启用状态，禁用其他
        if(DictValueEnum.START.getValue().equals(entity.getIsDisable())){
            PlanMixedLine planMixedLine = new PlanMixedLine();
            planMixedLine.setIsDisable(DictValueEnum.DISABLED.getValue());

            PlanMixedLine query = new PlanMixedLine();
            query.setLineCode(entity.getLineCode());
            query.setSectionCode(entity.getSectionCode());
            query.setIsDisable(DictValueEnum.START.getValue());
            planMixedLineMapper.update(planMixedLine,new QueryWrapper<>(query));
        }

        return planMixedLineMapper.insert(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateById(PlanMixedLineDto resources){
        PlanMixedLine entity = ConvertUtil.convert(resources, PlanMixedLine.class);

        //如果是启用状态，禁用其他
        if(DictValueEnum.START.getValue().equals(entity.getIsDisable())){

            PlanMixedLine mixed = planMixedLineMapper.selectById(entity.getId());
            if(!DictValueEnum.START.getValue().equals(mixed.getIsDisable())){
                PlanMixedLine planMixedLine = new PlanMixedLine();
                planMixedLine.setIsDisable(DictValueEnum.DISABLED.getValue());

                PlanMixedLine query = new PlanMixedLine();
                query.setLineCode(entity.getLineCode());
                query.setSectionCode(entity.getSectionCode());
                query.setIsDisable(DictValueEnum.START.getValue());
                planMixedLineMapper.update(planMixedLine,new QueryWrapper<>(query));
            }
        }
        int ret = planMixedLineMapper.updateById(entity);
        // delCaches(resources.id);
        return ret;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeByIds(Set<Integer> ids){
        // delCaches(ids);
        for (Integer id : ids) {
            PlanMixedRule planMixedRule = new PlanMixedRule();
            planMixedRule.setMixedLineId(id);
            planMixedRuleMapper.delete(new QueryWrapper<>(planMixedRule));
        }
        return planMixedLineMapper.deleteBatchIds(ids);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeById(Integer id){
        Set<Integer> set = new HashSet<>(1);
        set.add(id);
        return this.removeByIds(set);
    }

    /*
    private void delCaches(Integer id) {
        redisUtils.delByKey(CACHE_KEY + "::id:", id);
    }

    private void delCaches(Set<Integer> ids) {
        for (Integer id: ids) {
            delCaches(id);
        }
    }*/

    /*
    @Override
    public void download(List<PlanMixedLineDto> all, HttpServletResponse response) throws IOException {
      List<Map<String, Object>> list = new ArrayList<>();
      for (PlanMixedLineDto planMixedLine : all) {
        Map<String,Object> map = new LinkedHashMap<>();
              map.put("产线编码", planMixedLine.getLineCode());
              map.put("工段编码", planMixedLine.getSectionCode());
              map.put("是否禁用", planMixedLine.getIsDisable());
              map.put("描述", planMixedLine.getRemark());
              map.put("创建人", planMixedLine.getCreateBy());
              map.put("创建时间", planMixedLine.getCreateTime());
              map.put("更新人", planMixedLine.getUpdateBy());
              map.put("更新时间", planMixedLine.getUpdateTime());
        list.add(map);
      }
      FileUtil.downloadExcel(list, response);
    }*/
}
